package com.jmlee.pojo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ThisMonthWeekDate {


    public static List<TimeUpDown> getzxsMonth() {//获取本月五周的起止日期

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<WeekInfoModel> tt = getThisMonthWeekDate();
        List<TimeUpDown> zxsMonth = new ArrayList<>();

        for (int i = 0; i < tt.size(); i++) {
            TimeUpDown timeUpDown = new TimeUpDown();
            if (i == 0) {
                timeUpDown.setStartTime(format.format(getFirstDayOfMonth()));
                timeUpDown.setEndTime(format.format(tt.get(i).endDateOfWeek));
            } else if (i + 1 == tt.size()) {

                timeUpDown.setStartTime(format.format(tt.get(i).startDateOfWeek));
                timeUpDown.setEndTime(format.format(getLastDayOfMonth()));
            } else {
                timeUpDown.setStartTime(format.format(tt.get(i).startDateOfWeek));
                timeUpDown.setEndTime(format.format(tt.get(i).endDateOfWeek));

            }

            zxsMonth.add(timeUpDown);
        }


        return zxsMonth;

    }

    public static List<TimeUpDown> getBanYearsMonth() {//获取近半年每个月的起止日期

        List<TimeUpDown> zxsYears = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i > -6; i--) {
            TimeUpDown timeUpDown = new TimeUpDown();
            Calendar cal_1 = Calendar.getInstance();//获取当前日期
            cal_1.add(Calendar.MONTH, i);
            cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String firstDay = format.format(cal_1.getTime());
            timeUpDown.setStartTime(firstDay);
            //获取前月的最后一天
            Calendar cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, i + 1);//设置为1号,当前日期既为本月第一天
            cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
            String lastDay = format.format(cale.getTime());
            timeUpDown.setEndTime(lastDay);
            zxsYears.add(timeUpDown);
        }


        return zxsYears;
    }


    public static List<String> getMonthName() {//获取近半年每个月的具体月份
        List<String> month = new ArrayList<>();
        SimpleDateFormat f = new SimpleDateFormat("yyyy年M月");
        for (int i = 0; i > -6; i--) {
            TimeUpDown timeUpDown = new TimeUpDown();
            Calendar cal_1 = Calendar.getInstance();//获取当前日期
            cal_1.add(Calendar.MONTH, i);
            cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            month.add(f.format(cal_1.getTime()));
        }

        return month;
    }


    public static List<WeekInfoModel> getThisMonthWeekDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<WeekInfoModel> list = new ArrayList<WeekInfoModel>();

        // 1 获取本月的第一天
        Date firstDayOfMonth = getFirstDayOfMonth();
        Calendar firstDayOfMonthCal = Calendar.getInstance();
        firstDayOfMonthCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstDayOfMonthCal.setTime(firstDayOfMonth);

        // 2 获取本月第一周第一天
        Date firstWeekFirstDay = getFirstDayOfFirstWeekOfMonth();
        Calendar firstWeekFirstDayCal = Calendar.getInstance();
        firstWeekFirstDayCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstWeekFirstDayCal.setTime(firstWeekFirstDay);

        // 3 获取本次周期起止时间
        Date sDate = null;// 本月第一周第一天
        Date eDate = null;// 本月最后一周最后一天
        Calendar sCal = Calendar.getInstance();
        Calendar eCal = Calendar.getInstance();
        sCal.setFirstDayOfWeek(Calendar.MONDAY);
        sCal.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取本月第一天是周几
        int weekDay = firstDayOfMonthCal.get(Calendar.DAY_OF_WEEK) - 1;

        // 如果是周一，起始时间就是本月第一周第一天
        // 否则起始时间就是本月第一周第一天 倒退7天
        if (weekDay == 1) {// 周一

            sDate = firstWeekFirstDay;
        } else {
            firstWeekFirstDayCal.getTime();
            firstWeekFirstDayCal.add(Calendar.DATE, -7);
            sDate = firstWeekFirstDayCal.getTime();
        }
        sCal.setTime(sDate);

        // 结束时间是起始时间+34天（5周）
        eCal.setTime(sDate);
        eDate = eCal.getTime();
        eCal.add(Calendar.DATE, 34);
        eDate = eCal.getTime();

        Date cDate = new Date();
        if (cDate.getTime() < eDate.getTime()) {
            eDate = cDate;
        }
//--------------以下为修改部分-------------
        int yueri = getCurrentMonthLastDay();//获取当月天数
        int zhouci = 5;//默认一个月为五周
        if (yueri == 28 && weekDay == 1) {//总天数为28天 ,第一天为周六、周日、周一  当月为四周
            zhouci = 4;
        }
        // 4 循环得到周次信息
        for (int i = 0; i < zhouci; i++) {// 循环i周次
//--------------以上为修改部分-------------
            WeekInfoModel model = new WeekInfoModel();
            Calendar endTime = Calendar.getInstance();
            endTime.setFirstDayOfWeek(Calendar.MONDAY);
            endTime.setTime(sDate);
            endTime.set(Calendar.HOUR_OF_DAY, 23);
            endTime.set(Calendar.MINUTE, 59);
            endTime.set(Calendar.SECOND, 59);
            endTime.add(Calendar.DATE, 6);

            model.setIndexOfWeek(i + 1);
            model.setStartDateOfWeek(sDate);
            model.setEndDateOfWeek(endTime.getTime());

        /*System.out.println(" 本月第" + (i + 1) + "周起止时间是："
                + format.format(sDate) + " 到 "
                + format.format(endTime.getTime()));*/
            list.add(model);

            sCal.add(Calendar.DATE, 7);
            sDate = sCal.getTime();
        }

        return list;
    }

    public static Date getFirstDayOfMonth() {// 1 获取本月的第一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    public static Date getFirstDayOfFirstWeekOfMonth() {     // 2 获取本月第一周第一天

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int i = 1;
        while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            cal.set(Calendar.DAY_OF_MONTH, i++);
        }


        return cal.getTime();
    }


    public static int getCurrentMonthLastDay() {//获取当月天数
        Calendar a = Calendar.getInstance();

        a.set(Calendar.DATE, 1);

        a.roll(Calendar.DATE, -1);

        int maxDate = a.get(Calendar.DATE);

        return maxDate;
    }


    public static Date getLastDayOfMonth() {//获取本月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar2.getTime();
    }


}
